<template>
  <div class="lineChart">
    <div id="budgetLine" class="echart"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { LineChart } from "echarts/charts";
import { DataZoomComponent, VisualMapComponent } from "echarts/components"; // 平移缩放功能按需引入
echarts.use([LineChart, DataZoomComponent, VisualMapComponent]);
export default {
  name: "lineChat",
  props: {
    // 折线图数据
    lineData: {
      type: Array,
      default: [],
    },
    xData: {
      type: Array,
      default: [],
    },
    mapData: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.lineCharts(this.lineData, this.xData);
  },
  beforeDestroy() {
    this.chartDes();
  },
  watch: {
    lineData: {
      handler(val) {
        if (val) {
          this.chartDes();
          this.lineCharts(this.lineData, this.xData);
        }
      },
      deep: true,
      immediate: false,
    },
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    lineCharts(lineData, xData) {
      let option = {
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xData,
        },
        dataZoom: {
          type: "slider", // slider  inside
          show: true,
          zoomLock: false, // 如果设置为 true 只能平移，不能缩放
          // xAxisIndex: [0], // 控制哪个X轴 多个X轴的时候生效
          // left: "9%",
          // bottom: -5, // slider生效
          start: 0,
          end: 100, //初始化滚动条
        },
        grid: {
          left: "5%",
          right: "7%",
          bottom: "20%",
          top: "12%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
          name: "kw",
        },
        tooltip: {
          trigger: "axis",
          valueFormatter: function (value) {
            return value + 'kw';
          },
        },
        visualMap: {
          show: false,
          top: 0,
          right: 0,
          precision: 3, //设置小数精度，默认0没有小数
          pieces: this.mapData,
          outOfRange: {
            color: "#999",
          },
        },
        series: [
          {
            name: "实时负荷",
            data: lineData,
            showSymbol: false,
            type: "line",
            // lineStyle: {
            //   width: 1,
            //   color: "#FFC436",
            // },
            // areaStyle: {
            //   color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            //     {
            //       offset: 0,
            //       color: "#FFC436",
            //     },
            //     {
            //       offset: 1,
            //       color: "#ffffff",
            //     },
            //   ]),
            // },
          },
        ],
      };
      this.initChart("budgetLine", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.lineChart {
  .echart {
    width: 100%;
    height: 220px;
  }
}
</style>
